Creating a GG Plot of Top 5 SF trees legal status data
ggplot(data = top_5_status, aes(x = fct_reorder(legal_status, tree_count), y = tree_count)) +
# fct_reorder - reorders legal statuses from default alphabetical order to lowest - the greatest tree count
geom_col() +
labs(y= 'Tree Count', x = 'Legal Status') +
coord_flip() + # flips the axes
theme_minimal()

Keep obs where legal status is “Permitted Site” and caretaker is
“MTA”
# sf_trees$legal_status %>% unique()
permitted_mta <- sf_trees %>%
filter(legal_status == "Permitted Site" & caretaker =="MTA")
Keep Blackwood acacia trees, only legal_status, date, latitude,
longitude columns
blackwood_acacia <- sf_trees %>%
filter(str_detect(species, "Blackwood Acacia")) %>%
select(legal_status, date, latitude, longitude)
# Make a plot of SF acacia
ggplot(data = blackwood_acacia, aes(x = longitude, y = latitude)) +
geom_point()

Use the tidyr::separate() to separate one column into multiple
columns, and tidyr::unite() to rejoin
# Using separate to break column into species and scientific name
sf_trees_sep <- sf_trees %>%
separate(species, into = c('spp_scientific', 'spp_common'), sep = ' :: ')
sf_trees_unite <- sf_trees %>%
unite('id_status', tree_id:species) # or could use commas to join specific columns not in a row
Make some actual maps!
# Spacial / simple features map
blackwood_acacia_sf <- blackwood_acacia %>%
drop_na(longitude, latitude) %>%
st_as_sf(coords = c('longitude', 'latitude')) # from the sf package - turn spacial & temporal data and turn it into a geometry feature (a point)
st_crs(blackwood_acacia_sf) <- 4326
# telling R this coordinate reference system is important
# numeric code - indicates basic latitude and longitude coordinate reference system in degrees
ggplot(data = blackwood_acacia_sf) +
geom_sf(color = 'darkgreen') + # plots geometrical special features (always looks for the geometry column as the aesthetics to map the x & y)
theme_minimal()

Read in SF streets data
sf_map_sf <- read_sf(here('data', 'sf_map','tl_2017_06075_roads.shp')) %>% # reads in spacial data as a simple feature
# st_crs(sf_map_sf) # tells us about the crs
# want to change the crs (before we knew what it was so we could just type it in, but this one is different)
# we want to tranform it into the one we want
st_transform(4326)
st_crs(sf_map_sf) # now it's where we want it to be
## Coordinate Reference System:
## User input: EPSG:4326
## wkt:
## GEOGCRS["WGS 84",
## ENSEMBLE["World Geodetic System 1984 ensemble",
## MEMBER["World Geodetic System 1984 (Transit)"],
## MEMBER["World Geodetic System 1984 (G730)"],
## MEMBER["World Geodetic System 1984 (G873)"],
## MEMBER["World Geodetic System 1984 (G1150)"],
## MEMBER["World Geodetic System 1984 (G1674)"],
## MEMBER["World Geodetic System 1984 (G1762)"],
## MEMBER["World Geodetic System 1984 (G2139)"],
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]],
## ENSEMBLEACCURACY[2.0]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Horizontal component of 3D system."],
## AREA["World."],
## BBOX[-90,-180,90,180]],
## ID["EPSG",4326]]
Let’s create a plot now
ggplot() +
geom_sf(data = sf_map_sf, size = 0.1, color = 'darkgrey') +
geom_sf(data = blackwood_acacia_sf, color =
'red', size = 0.5) +
theme_void() +
labs(title = "Blackwood acacias in San Francisco")

Interactive map!
tmap_mode('view')
tm_shape(blackwood_acacia_sf) +
tm_dots()
# creating a shape using this data with dot shapes